package fragment;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.studyAndroid.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import ui.FragmentAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * note:
 * User: Shaxj
 * Date: 15-6-29
 * Time: 上午10:09
 */
public class IndexFragment extends Fragment {

    private View rootView;
    private PullToRefreshListView pullToRefreshListView;
    private FragmentAdapter adapter;
    private int i = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        super.onCreateView(inflater, container, savedInstanceState);
        rootView = inflater.inflate(R.layout.indexfragment, container, false);
        Bundle arguments = getArguments();
        String data = arguments.getString("data");
        init();
        return rootView;
    }

    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String[] result) {
            getDate();
            pullToRefreshListView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }

    public List<String> getResult() {
        List<String> xx = new ArrayList<String>();
        xx.add("ThinkPad S4" + "__" + i);
        xx.add("ThinkPad S5" + "__" + i);
        xx.add("ThinkPad T440" + "__" + i);
        xx.add("iPad mini" + "__" + i);
        xx.add("iPad air" + "__" + i);
        xx.add("new iPad" + "__" + i);
        i++;
        return xx;
    }

    public void getDate() {
        List<List<String>> result = new ArrayList<List<String>>();
        if (isFirst) {
            for (int i = 0; i < 10; i++) {
                result.add(getResult());
            }
        }
        isFirst=false;
        result.add(getResult());
        if (null == adapter) {
            adapter = new FragmentAdapter(getActivity(), result);
            pullToRefreshListView.setAdapter(adapter);
        } else {
            adapter.setResult(result);
            adapter.notifyDataSetChanged();
        }
    }

    private void init() {
        pullToRefreshListView = (PullToRefreshListView) rootView.findViewById(R.id.pull_refresh_list);
        //添加监听
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(getActivity(), System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                isFirst=true;
                // Update the LastUpdatedLabel
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                refreshView.getLoadingLayoutProxy().setRefreshingLabel("正在刷新");
                refreshView.getLoadingLayoutProxy().setPullLabel("下拉刷新");
                refreshView.getLoadingLayoutProxy().setReleaseLabel("释放开始刷新");
                new GetDataTask().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                String label = DateUtils.formatDateTime(getActivity(), System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                // Update the LastUpdatedLabel
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                refreshView.getLoadingLayoutProxy().setRefreshingLabel("正在刷新");
                refreshView.getLoadingLayoutProxy().setPullLabel("获取数据");
                refreshView.getLoadingLayoutProxy().setReleaseLabel("释放开始获取!");
                new GetDataTask().execute();
            }

        });
        //再获取数据
        pullToRefreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                new GetDataTask().execute();
            }
        });
        // 点击事件
        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "点击事件！", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    Boolean isFirst = true;

    @Override
    public void onResume() {
        super.onResume();
        getDate();
    }
}
